<script setup>
import { ref, reactive } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";
const data = reactive({
  typeList: [],
  type: {
    type: "",
  },
});
const addType = ref(false);
const handleClose = () => {
  addType.value = false;
};
const getTypeList = () => {
  axios.get("/type/getTypeList").then((res) => {
    data.typeList = res.data.data;
  });
};
const insertType = () => {
  axios.post("/type/insertType", data.type).then((res) => {
    if (res.data.code == 200) {
      addType.value = false;
      ElMessage.success("添加成功");
    } else {
      ElMessage.error(res.data.message);
    }
    getTypeList();
  });
};
const deleteType = (id)=>{
  axios.delete('/type/deleteType/'+ id).then(res=>{
    ElMessage.success("删除成功");
    getTypeList();
  })
}
getTypeList();
</script>
<template>
  <div>
    <el-button class="fr" type="primary" @click="addType = true"
      >添加类别</el-button
    >
    <div style="margin: 10px">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item>后台管理</el-breadcrumb-item>
        <el-breadcrumb-item>投票类型管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-table :data="data.typeList" stripe border>
      <el-table-column type="index" label="序号" />
      <el-table-column prop="type" label="类型" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column fixed="right" label="操作" width="220">
        <template #default="scope">
          <el-tooltip
            class="box-item"
            content="编辑"
            placement="top"
            effect="light"
          >
            <el-button type="primary" @click="">
              <el-icon>
                <Edit />
              </el-icon>
            </el-button>
          </el-tooltip>
          <el-tooltip
            class="box-item"
            content="删除"
            placement="top"
            effect="light"
          >
            <el-button type="danger" @click="deleteType(scope.row.id)">
              <el-icon>
                <Delete />
              </el-icon>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block"></div>
    <el-dialog
      v-model="addType"
      title="添加类别"
      width="30%"
      :before-close="handleClose"
    >
      <span>类别名称：</span>
      <el-input
        v-model="data.type.type"
        placeholder="请输入类别名称"
        size="normal"
        clearable
        style="margin-top: 10px"
      >
      </el-input>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addType = false">取消</el-button>
          <el-button type="primary" @click="insertType()">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped>
.block {
  margin-top: 20px;
}

.fr {
  float: right;
}
</style>
